๐Ÿ‘ฉโ€๐Ÿ’ป 92341. ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ

@Seongukbaek Posted July 06, 2022 ยท Updated July 06, 2022 ยท 4 min read

๋ฌธ์ œ

Programmers - ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ

๐Ÿ“ Logic

// ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ์ž…์ฐจ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” map static Map<String, Integer> carTimeMap = new HashMap<>(); // ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” map static Map<String, Integer> accumCarMap = new HashMap<>();
  • ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ์ž…์ฐจ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” map์„ ์‚ฌ์šฉํ•ด, ์ž…์ฐจ ์‹œ์—๋Š” map์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ถœ์ฐจ ์‹œ์—๋Š” ์ž…์ฐจ ์‹œ๊ฐ„๊ณผ ๋น„๊ตํ•˜์—ฌ ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค.
  • ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” map์„ ์‚ฌ์šฉํ•ด ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ๋ˆ„์ ํ•ฉํ•œ๋‹ค.
readRecords(records); processRemainingCars(); calculateFees();
  • readRecords() ๋Š” ์ฃผ์ฐจ ๊ธฐ๋ก์„ ์ฝ์œผ๋ฉด์„œ ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•œ๋‹ค.
  • processRemainingCars() ์—์„œ๋Š” ์ถœ์ฐจ ๊ธฐ๋ก์ด ์—†๋Š” ์ฐจ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • calculateFees() ์—์„œ๋Š” ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ์ฃผ์ฐจ ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•  ๋ฐฐ์—ด์— ๋‹ด๋Š”๋‹ค.

๐Ÿ“„ CODE

์ฝ”๋“œ ๋ณด๊ธฐ/์ ‘๊ธฐ๐Ÿ’ซ
import java.util.*; class Solution { static int basicTime; static int basicFee; static int unitTime; static int unitFee; // ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ์ž…์ฐจ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” map static Map<String, Integer> carTimeMap = new HashMap<>(); // ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” map static Map<String, Integer> accumCarMap = new HashMap<>(); public int[] solution(int[] fees, String[] records) { init(fees); readRecords(records); processRemainingCars(); return calculateFees(); } private static void init(int[] fees) { basicTime = fees[0]; basicFee = fees[1]; unitTime = fees[2]; unitFee = fees[3]; } private static void readRecords(String[] records) { for (String record : records) { String[] info = record.split(" "); int time = timeToInt(info[0]); String carN = info[1]; boolean in = Objects.equals(info[2], "IN"); // ์ž…์ฐจํ•œ ๊ฒฝ์šฐ if (in) carTimeMap.put(carN, time); // ์ถœ์ฐจํ•œ ๊ฒฝ์šฐ else { // ์ž…์ฐจ ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ด int inTime = carTimeMap.get(carN); // ์ฃผ์ฐจ ์‹œ๊ฐ„ ๊ณ„์‚ฐ time -= inTime; accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time); carTimeMap.remove(carN); } } } private static int timeToInt(String info) { String[] line = info.split(":"); int hour = Integer.parseInt(line[0]) * 60; int min = Integer.parseInt(line[1]); return hour + min; } // ์ถœ์ฐจ ๊ธฐ๋ก์ด ์—†๋Š” ์ฐจ ์ฒ˜๋ฆฌ private static void processRemainingCars() { for (Map.Entry<String, Integer> entry : carTimeMap.entrySet()) { String carN = entry.getKey(); int time = 1439 - entry.getValue(); accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time); } } // ์ฐจ ๋ฒˆํ˜ธ๋ณ„ ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ์ด์šฉํ•ด ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ private static int[] calculateFees() { // ์ฐจ ๋ฒˆํ˜ธ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ Object[] keys = accumCarMap.keySet().toArray(); Arrays.sort(keys); int[] answer = new int[accumCarMap.size()]; int idx = 0; // ์ •๋ ฌ๋œ ์ฐจ ๋ฒˆํ˜ธ ์ˆœ์œผ๋กœ ๋ˆ„์  ์š”๊ธˆ ๊ณ„์‚ฐ for (Object key : keys) { int time = accumCarMap.get(key.toString()); int fee = basicFee; if (time > basicTime) { time = (int) Math.ceil((double) (time - basicTime) / unitTime); fee += (time * unitFee); } answer[idx] = fee; idx++; } return answer; } }

โœ๏ธ Review

  • ํ•˜๋ผ๋Š” ๋Œ€๋กœ ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€์œผ๋‚˜, ์ž๊พธ 37.5์ ์ด ๋‚˜์˜ค๊ธธ๋ž˜ map ์— ์ ‘๊ทผํ•˜๋Š” ์ฝ”๋“œ๋“ค์„ ๋‹ค ๋œฏ์–ด๋ณด๋‹ค๊ฐ€ ์–ด์ด์—†๋Š” ์‹ค์ˆ˜๋ฅผ ํ•œ ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค.
// ์ถœ์ฐจ ๊ธฐ๋ก์ด ์—†๋Š” ์ฐจ ์ฒ˜๋ฆฌ private static void processRemainingCars() { int time = 1439; for (Map.Entry<String, Integer> entry : carTimeMap.entrySet()) { String carN = entry.getKey(); time = time - entry.getValue(); accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time); } } // ์ถœ์ฐจ ๊ธฐ๋ก์ด ์—†๋Š” ์ฐจ ์ฒ˜๋ฆฌ private static void processRemainingCars() { for (Map.Entry<String, Integer> entry : carTimeMap.entrySet()) { String carN = entry.getKey(); int time = 1439 - entry.getValue(); accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time); } }
  • time ๊ฐ’์€ 1439๋กœ ๊ณ ์ •๋˜์–ด ์žˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ์ž๊พธ ๊ฐ์†Œํ•˜๋„๋ก ๊ตฌํ˜„ํ•ด๋†“์•„์„œ ํ‹€๋ ธ๋˜ ๊ฒƒ์ด๋‹ค...
@Seongukbaek
๋ฐฐ์šฐ๊ณ  ์ •๋ฆฌํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋Œ€ํ•™์ƒ ๋ฐฑ์„ฑ์šฑ์ž…๋‹ˆ๋‹ค ๐Ÿ˜€